<!-- 
  MIT License

  Copyright (c) 2023 小辉不会飞

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
-->
<!-- advancedViewPositioning 视图定位进阶 -->
<template>
  <div class="container">
    <main-header showBack :title="route.query.name"></main-header>
    <div class="main">
      <div id="map" class="map"></div>
      <div class="btns flex flex-hc">
        <el-button @click="zoomToShaanxi">缩放到陕西</el-button>
        <el-button @click="zoomToXian">缩放到西安</el-button>
        <el-button @click="centerXian">以西安为中心</el-button>
      </div>
      <!-- 地图遮罩层 -->
      <div class="padding-top"></div>
      <div class="padding-left"></div>
      <div class="padding-right"></div>
      <div class="padding-bottom"></div>
      <div class="center"></div>
    </div>
    <main-footer></main-footer>
  </div>
</template>

<script setup>
// VUE相关
import { onMounted, reactive } from "vue";
import { useRoute } from "vue-router";
// 项目组件
import mainHeader from "@/components/mainHeader.vue";
import mainFooter from "@/components/mainFooter.vue";
// OL相关
import GeoJSON from "ol/format/GeoJSON";
import Map from "ol/Map";
import View from "ol/View";
import { OSM, Vector as VectorSource } from "ol/source";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import "ol/ol.css";
// 依赖
import { loadVecTdt } from "@/common/loadTDT";

const route = useRoute(); // 路由对象

const ol = reactive({
  map: null,
  source: null,
  vectorLayer: null,
});

onMounted(() => {
  getVectorLayer();
  // initMap(); // 国内osm无法访问，此处替换成天地图
  ol.map = loadVecTdt("map", [ol.vectorLayer]);
});

/**
 * 获取矢量图层
 */
const getVectorLayer = () => {
  ol.source = new VectorSource({
    url: "./data/shaanxi.geojson",
    format: new GeoJSON(),
  });
  ol.vectorLayer = new VectorLayer({
    source: ol.source,
    style: {
      "fill-color": "rgba(255, 255, 255, 0.6)",
      "stroke-width": 1,
      "stroke-color": "#319FD3",
      "circle-radius": 5,
      "circle-fill-color": "rgba(255, 255, 255, 0.6)",
      "circle-stroke-width": 1,
      "circle-stroke-color": "#319FD3",
    },
  });
};

/**
 * 初始化地图
 */
const initMap = () => {
  // 初始化地图到指定DOM元素
  ol.map = new Map({
    layers: [
      new TileLayer({
        source: new OSM(),
      }),
      ol.vectorLayer,
    ],
    target: "map",
    view: new View({
      center: [0, 0],
      zoom: 1,
    }),
  });
};

/**
 * 缩放到陕西
 */
const zoomToShaanxi = () => {
  const feature = ol.source.getFeatures()[0];
  const polygon = feature.getGeometry();
  ol.map.getView().fit(polygon, { padding: [170, 50, 30, 150] });
};

/**
 * 缩放到西安
 */
const zoomToXian = () => {
  const feature = ol.source.getFeatures()[1];
  const point = feature.getGeometry();
  ol.map
    .getView()
    .fit(point, { padding: [170, 50, 30, 150], minResolution: 50 });
};

/**
 * 以西安为中心
 */
const centerXian = () => {
  const feature = ol.source.getFeatures()[1];
  const point = feature.getGeometry();
  const size = ol.map.getSize();
  ol.map
    .getView()
    .centerOn(point.getCoordinates(), size, [560 + 10, 500 + 10 - 54]);
};
</script>

<style scoped lang="scss">
#map {
  width: 100%;
  height: calc(100% - 60px);
  :deep(.ol-zoom) {
    top: 178px;
    left: 158px;
  }
  :deep(.ol-rotate) {
    top: 178px;
    right: 58px;
  }
  :deep(.ol-attribution),
  :deep(.ol-attribution.ol-uncollapsible) {
    bottom: 30px;
    right: 50px;
  }
}
.padding-top {
  position: absolute;
  top: 54px;
  left: 0px;
  width: 100%;
  height: 170px;
  background: rgba(255, 255, 255, 0.5);
}
.padding-left {
  position: absolute;
  top: 224px;
  left: 0;
  width: 150px;
  height: calc(100% - 54px - 32px - 60px - 170px - 30px);
  background: rgba(255, 255, 255, 0.5);
}
.padding-right {
  position: absolute;
  top: 224px;
  right: 0;
  width: 50px;
  height: calc(100% - 54px - 32px - 60px - 170px - 30px);
  background: rgba(255, 255, 255, 0.5);
}
.padding-bottom {
  position: absolute;
  bottom: 92px;
  left: 0px;
  width: 100%;
  height: 30px;
  background: rgba(255, 255, 255, 0.5);
}
.center {
  position: absolute;
  border: solid 1px black;
  top: 500px;
  left: 560px;
  width: 20px;
  height: 20px;
}
.btns {
  height: 60px;
  padding: 0 15px;
}
</style>
